package com.yangsir.mysite.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.yangsir.mysite.properties.AliyunProperties;
import com.yangsir.mysite.utils.AlimsgTemple;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * java 8
 */
@Configuration
@EnableConfigurationProperties(AliyunProperties.class)
public class AliyunClient {
    @Bean
    public AsyncClient build(AliyunProperties aliyunProperties){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliyunProperties.getAccessKeyId())
                .accessKeySecret(aliyunProperties.getAccessKeySecret())
                .build());

        AsyncClient client = AsyncClient.builder()
                .region(aliyunProperties.getRegion()) // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        return client;
    }
    @Bean
    public AlimsgTemple sendSms(AliyunProperties aliyunProperties, AsyncClient client){
        return new AlimsgTemple(aliyunProperties, client);
    }

}
